﻿/////////////////////////////////////////////////////////////////
//File: BlogInfo.cs
//Description: 
//Copyright© 2011 Moliu
//History:
//  2011-3-14 0:38:21    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using MAF.Common;
using System.Security.Principal;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Persistence;


namespace My.Blog.Business.Security
{
    /// <summary>
    /// IIdentity
    /// </summary>
    public class BlogIdentity : IIdentity
    {

        #region 构造函数
        
        /// <summary>
        /// BlogIdentity
        /// </summary>
        /// <param name="id">有两层含义,id 和 owner</param>
        /// <param name="password"></param>
        public BlogIdentity(string id,string password)
        {
            int identity; int.TryParse( id, out identity );
            _Entity.Id = identity;
            _Entity.Owner = id;
            _Entity.Password = password;
            if (null == _Entity.Password)
                _Entity.Password = string.Empty;  // 如果密码为null,sql中将忽略密码的检查

            _Entity = _Blog.ReadBlogByConditions(_Entity);
            if (null != _Entity)
            {
                _IsAuthenticated = true;   //登录成功
                _Name = _Entity.Id.ToString();
            }
        }

        #endregion

        #region 属性
        public BlogInfo Entity
        {
            get { return _Entity; }
        }
        #endregion

        #region IIdentity 成员

        public string AuthenticationType
        {
            get { return _AuthenticationType; }
        }
        
        public bool IsAuthenticated
        {
            get { return _IsAuthenticated; }
        }

        public string Name
        {
            get { return _Name; }
        }

        #endregion

        #region Protected Members
        protected string _AuthenticationType = "Blog";
        protected bool _IsAuthenticated = false;
        protected string _Name;
        protected BlogInfo _Entity = new BlogInfo();

        // 此段代码在BlogManager中有重复
        private IBlog _Blog = new BlogDao("SqlMap_Blog.config");
        #endregion
    }
}